<?php

namespace app\api\controller;

use app\api\model\Courses;
use app\api\model\GardenImages;
use app\api\model\Gardens;
use app\api\model\LearningImages;
use app\api\model\Learnings;
use app\api\model\MonthReports;
use app\api\model\Recipes;
use app\api\model\TermReports;
use app\common\controller\Api;
use think\Db;
use think\helper\Time;

class Record extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 课程
     */
    public function course()
    {
        $date = $this->request->get('date');
        $class_id = $this->request->get('class_id');
        if (empty($date) || !$class_id) {
            $this->error('时间和班级不能为空');
        }
        $courses = Courses::getCourse($class_id, $date);
        $courses ? $this->success('请求成功', ['courses' => $courses]) : $this->error('没有数据');
    }

    /**
     * 食谱
     */
    public function recipe()
    {
        $date = $this->request->get('date');
        if (empty($date)) {
            $this->error('时间不能为空');
        }
        $recipes = Recipes::getRecipe($date);
        $recipes ? $this->success('请求成功', ['recipes' => $recipes]) : $this->error('没有数据');
    }

    /**
     * 学习情况
     */
    public function getLearning($student_id)
    {
        $date = $this->request->get('date');
        if (empty($date)) {
            $this->error('时间不能为空');
        }
        $learning = Learnings::getByStudent($student_id, $date);
        $learning ? $this->success('请求成功', ['learning' => $learning]) : $this->error('没有数据');
    }

    /**
     *  在园情况
     */
    public function getGarden($student_id)
    {
        $date = $this->request->get('date');
        if (empty($date)) {
            $this->error('时间不能为空');
        }
        $garden = Gardens::getByStudent($student_id, $date);
        $garden ? $this->success('请求成功', ['garden' => $garden]) : $this->error('没有数据');
    }

    /**
     * 月报
     */
    public function getMonthReport($student_id)
    {
        $date = $this->request->get('date');
        if (empty($date)) {
            $this->error('时间不能为空');
        }
        $month_report = MonthReports::getByStudent($student_id, $date);
        $month_report ? $this->success('请求成功', ['month_report' => $month_report]) : $this->error('没有数据');
    }


    /**
     * 学期报
     */
    public function getTermReport($student_id)
    {
        $date = $this->request->get('date');
        $type = $this->request->get('type');
        if (!$date || empty($type)) {
            $this->error('时间和学期都不能为空');
        }
        $term_report = TermReports::getByStudent($student_id, $date, $type);
        $term_report ? $this->success('请求成功', ['term_report' => $term_report]) : $this->error('没有数据');
    }


    /**
     * 添加学习情况
     */
    public function createLearning($student_id)
    {
        $data = $this->request->post();
        if (!isset($data['reading']) || empty($data['reading'])) {
            $this->error('阅读情况不能为空');
        }
        Db::startTrans();
        try {
            $learning = Learnings::createLearning($student_id, $data);
            if (isset($data['knowledge_images']) && $data['knowledge_images']) {
                LearningImages::createImage($learning->id, $data['knowledge_images'], 'knowledge');
            }
            if (isset($data['homework_images']) && $data['homework_images']) {
                LearningImages::createImage($learning->id, $data['homework_images'], 'homework');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        isset($e) ? $this->error('添加失败') : $this->success('添加成功');
    }

    /**
     * 编辑学习情况
     */
    public function updateLearning($learning_id)
    {
        $data = $this->request->post();
        if (!isset($data['reading']) || empty($data['reading'])) {
            $this->error('阅读情况不能为空');
        }
        $learning = Learnings::find($learning_id);
        if (!$learning) {
            $this->error('学习情况还没有，请先添加');
        }
        Db::startTrans();
        try {
            Learnings::updateLearning($learning_id, $data);
            if (isset($data['knowledge_images'])) {
                LearningImages::updateImages($learning_id, $data['knowledge_images'], 'knowledge');
            }
            if (isset($data['homework_images'])) {
                LearningImages::updateImages($learning_id, $data['homework_images'], 'homework');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        isset($e) ? $this->error('更新失败') : $this->success('更新成功');
    }


    /**
     * 添加在园情况
     */
    public function createGarden($student_id)
    {
        $data = $this->request->post();
        if (!isset($data['eating']) || empty($data['eating']) || !isset($data['water']) || empty($data['water']) || !isset($data['washroom']) || empty($data['washroom'])) {
            $this->error('用餐、饮水、如厕情况都不能为空');
        }
        Db::startTrans();
        try {
            $garden = Gardens::createGarden($student_id, $data);
            if (isset($data['action_images']) && $data['action_images']) {
                GardenImages::createImage($garden->id, $data['action_images'], 'action');
            }
            if (isset($data['summary_images']) && $data['summary_images']) {
                GardenImages::createImage($garden->id, $data['summary_images'], 'summary');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        isset($e) ? $this->error('添加失败') : $this->success('添加成功');
    }

    /**
     * 编辑在园情况
     */
    public function updateGarden($garden_id)
    {
        $data = $this->request->post();
        if (!isset($data['eating']) || empty($data['eating']) || !isset($data['water']) || empty($data['water']) || !isset($data['washroom']) || empty($data['washroom'])) {
            $this->error('用餐、饮水、如厕情况都不能为空');
        }
        $garden = Gardens::find($garden_id);
        if (!$garden) {
            $this->error('在园情况还没有，请先添加');
        }
        Db::startTrans();
        try {
            Gardens::updateGarden($garden_id, $data);
            if (isset($data['action_images'])) {
                GardenImages::updateImages($garden_id, $data['action_images'], 'action');
            }
            if (isset($data['summary_images'])) {
                GardenImages::updateImages($garden_id, $data['summary_images'], 'summary');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        isset($e) ? $this->error('更新失败') : $this->success('更新成功');
    }

}
